﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace DT_KH
{
    public partial class _Default : System.Web.UI.Page
    {
        ServiceReference1.Service1Client service = new ServiceReference1.Service1Client();
        public static string Action = "new";
        public static Int64 txtid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                load_gridview();
            }
        }
        public void load_gridview()
        {
            Grid1.DataSource = service.get_all_KH_NGHIEP_VU();
            Grid1.DataBind();
        }

        protected void bt_luu_Click(object sender, EventArgs e)
        {
            if (Action == "new")
            {
                string ma_nv = txt_ma_nv.Text;
                string ten_nv = txt_ten_nv.Text;
                DateTime ngay_bd = DateTime.ParseExact(txt_ngay_bd.Text, "dd/MM/yyyy", null);
                bool result = service.insert_KH_NGHIEP_VU(ma_nv, ten_nv, ngay_bd);
                if (result == true)
                {
                    load_gridview();
                    txt_ma_nv.Text = "";
                    txt_ten_nv.Text = "";
                    txt_ngay_bd.Text = "";
                }
                
            }
            else
            {
                if (Grid1.SelectedRecords != null)
                {
                    string ma_nv = txt_ma_nv.Text;
                    string ten_nv = txt_ten_nv.Text;
                    DateTime ngay_bd = DateTime.ParseExact(txt_ngay_bd.Text, "dd/MM/yyyy", null);
                    foreach (Hashtable oRecord in Grid1.SelectedRecords)
                    {
                        txtid = Convert.ToInt32(oRecord["ID"]);
                    }
                    bool result = service.edit_KH_NGHIEP_VU(txtid, ma_nv, ten_nv,ngay_bd);
                    if (result == true)
                    {
                        load_gridview();
                        Action = "new";
                        txt_ma_nv.Text = "";
                        txt_ten_nv.Text = "";
                        txt_ngay_bd.Text = "";
                    }
                }
                else
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Error", "alert('Không lấy được dữ liệu nghiệp vụ');", true);
                }
            }
        }


        protected void bt_them_Click(object sender, EventArgs e)
        {
            Action = "new";
            txt_ma_nv.Text = "";
            txt_ten_nv.Text = "";
            txt_ngay_bd.Text = "";
        }

        protected void bt_xoa_Click(object sender, EventArgs e)
        {
            if (Grid1.SelectedRecords != null)
            {
                foreach (Hashtable oRecord in Grid1.SelectedRecords)
                {
                    txtid = Convert.ToInt32(oRecord["ID"]);
                }
                bool result = service.del_KH_NGHIEP_VU(txtid);
                if (result == true)
                {
                    load_gridview();
                    Action = "new";
                    txt_ma_nv.Text = "";
                    txt_ten_nv.Text = "";
                    txt_ngay_bd.Text = "";
                }
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Error", "alert('Không lấy được dữ liệu nghiệp vụ');", true);
            }

            
        }

        protected void Grid1_Rebind(object sender, EventArgs e)
        {
            load_gridview();
        }

        //protected void Grid1_Select(object sender, Obout.Grid.GridRecordEventArgs e)
        //{
        //    if (Grid1.SelectedRecords != null)
        //    {
        //        Action = "edit";
        //    }

        //}

        protected void bt_timkiem_Click(object sender, EventArgs e)
        {
            string txt_key = txt_tk.Text;
            Grid1.DataSource = service.search_kh_nghiep_vu(txt_key);
            Grid1.DataBind();
        }

        protected void Grid1_Select(object sender, Vdc.Grid.GridRecordEventArgs e)
        {
            if (Grid1.SelectedRecords != null)
            {
                Action = "edit";
            }
        }
        
    }
}
